package com.seabook.store.admin.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.seabook.store.auth.domain.User;
import com.seabook.store.auth.web.UserSession;
import com.seabook.store.purchase.domain.Order;
import com.seabook.store.purchase.domain.OrderUpdate;
import com.seabook.store.purchase.service.OrderService;
import com.seabook.store.util.ServiceLocator;

@Controller
public class AdminController {
    private final OrderService orderService; 
    

    public AdminController() {
        this.orderService = (OrderService) ServiceLocator
                .getBean("orderService");
    }
    
    @RequestMapping(value = "/admin/home")
    public String home(HttpServletRequest request){
        List<Order> orders = orderService.getOrderByStatus(Order.Status.PAID);
        request.setAttribute("paidOrders", orders);
        return "admin";
    }
    
    @RequestMapping(value="/admin/updateorderstatus")
    public String updateOrderStatus(Order order, HttpServletRequest request){
        User user = UserSession.getUserSession(request).getUser();
        orderService.updateOrderStatus(order, user);
        return "viewOrder";
    }
    
    @RequestMapping(value="/admin/updateorder")
    public String updateOrder(OrderUpdate orderUpdate, HttpServletRequest request){
        User user = UserSession.getUserSession(request).getUser();
        orderUpdate.setUserId(user.getUserId());
        orderUpdate.setUserName(user.getUserName());
        orderService.updateOrder(orderUpdate);
        return "viewOrder";
    }
    
    
}
